#include "BeginGraph.h"
#include "base/Utility.h"
#include "base/TrueTypeFont.h"
#include "OutputPin.h"
#define SetAttr(AttrName) SetAttribute(AttrName, ele->Attribute(AttrName));
bool BeginGraph::OnInit()
{
    GraphBase* n = context.FindGraphById(nextGraphId);
    if (n != nullptr)
    {
        nextGraph = n;
    }
    else {
        printf("BeginGraph not find nextGraph:%d\r\n", nextGraphId);
    }
    return true;
}

void BeginGraph::ParseXmlElement(TiXmlElement* ele)
{
    SetAttr("pos")
        SetAttr("size")
        SetAttr("onNext")
}

bool BeginGraph::SetAttribute(const char* attrName, const char* attrVal)
{
    /*  if (strcmp("id", attrName) == 0)
      {
          id.val = atoi(attrVal);
      }
      else*/ if (strcmp("name", attrName) == 0)
      {
          graphName = attrVal;
      }
      else if (strcmp("pos", attrName) == 0)
      {
          int posX, posY;
          sscanf_s(attrVal, "%d,%d", &posX, &posY);
          pos.x.val = posX;
          pos.y.val = posY;
      }
      else if (strcmp("size", attrName) == 0)
      {
          int x, y;
          sscanf_s(attrVal, "%d,%d", &x, &y);
          size.x.val = x;
          size.y.val = y;
      }
      else if (strcmp("onNext", attrName) == 0)
      {
          nextGraphId = atoi(attrVal);
      }
      return true;
}

void BeginGraph::OnDraw()
{
    if (op == nullptr)
    {
        op = NewObj<OutputPin>();
        op->gbParent = this;
    }
    glPushMatrix();
    glTranslatef(baseOffset.x.val, baseOffset.y.val, zIndex.val);
    glLineWidth(1);
    DrawBorder();

    //glColor3f(1, 1, 1);
    //glBegin(GL_LINE_LOOP);
    //// -6,-4 6,4 18,0
    //glVertex3f(pos.x.val + size.x.val - 20 - 6, pos.y.val + 15 - 4, 0);
    //glVertex3f(pos.x.val + size.x.val - 20 + 6, pos.y.val + 15 - 4, 0);
    //glVertex3f(pos.x.val + size.x.val - 20 + 18, pos.y.val + 15, 0);
    //glVertex3f(pos.x.val + size.x.val - 20 + 6, pos.y.val + 15 + 4, 0);
    //glVertex3f(pos.x.val + size.x.val - 20 - 6, pos.y.val + 15 + 4, 0);
    //glEnd();

    op->pos.x = pos.x.val + size.x.val - 20;
    op->pos.y = pos.y.val  + 165;

    op->Draw();
    glColor3f(1, 1, 1);
    std::wstring wstr = Utility::ConvertToWstring(graphName.val);
    wchar_t* str = const_cast<wchar_t*>(wstr.data());
    TrueTypeFont::GetInst()->drawText(str, pos.x.val + size.x.val / 4, pos.y.val + 50,zIndex.val, size.x.val, 20);
    glPopMatrix();


}

void BeginGraph::onMouseButtonDown(int button, int x, int y)
{
    __super::onMouseButtonDown(button, x, y);
    if (op != nullptr)
    {
        Vec2vbi point;
        point.x.val = x;
        point.y.val = y;
        if (op->HitTest(vec2(point.x.val, point.y.val)))
        {
            op->onMouseButtonDown(button, x, y);
        }
    }
}

void BeginGraph::onMouseMove(int x, int y)
{
    __super::onMouseMove(x, y);
}

void BeginGraph::onMouseButtonUp(int button, int x, int y)
{
    __super::onMouseButtonUp(button, x, y);
}

BlueprintScriptLibrary::Class* BeginGraph::GetClass()
{
    return nullptr;
}

Vec2vbi BeginGraph::GetOutputPinPos(int index)
{
    return Vec2vbi();
}

void BeginGraph::InitConnect(std::vector<GraphBase*>* allGraphs)
{

}

void BeginGraph::Execute()
{
    if (nextGraph)
    {
        nextGraph->Execute();
    }
}

FunctionPin* BeginGraph::GetFunctionPin()
{
    return nullptr;
}

void BeginGraph::ConnectTo(GraphBase* graph, int index)
{
    if (GetOuputPinNum().val > index)
    {
        nextGraph = graph;
    }
}

